//
//  StringExtension.swift
//  U17
//
//  Created by charles on 2017/10/9.
//  Copyright © 2017年 None. All rights reserved.
//

import Foundation

extension String {
    var trim:String {
        let whitespace = NSCharacterSet.whitespaces
        return self.trimmingCharacters(in: whitespace)
    }
    public func substring(from index: Int) -> String {
        if self.count > index {
            let startIndex = self.index(self.startIndex, offsetBy: index)
            let subString = self[startIndex..<self.endIndex]
            return String(subString)
        } else {
            return self
        }
    }
    func isTelNumber()->Bool{
        let mobile = "^[1][3,4,5,6,7,8,9][0-9]{9}$"
        let regextestmobile = NSPredicate(format: "SELF MATCHES %@",mobile)
        if ((regextestmobile.evaluate(with: self) == true)){
            return true
        }else{
            return false
        }
    }
    func isIDCard()->Bool{
        let idCard = "^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$"
        let regextestmobile = NSPredicate(format: "SELF MATCHES %@",idCard)
        if ((regextestmobile.evaluate(with: self) == true)){
            return true
        }else{
            return false
        }
    }
}
